﻿using FPSO.HMI.Controls;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FPSO.HMI.Utils
{
    /// <summary>
    /// 报警工具类
    /// </summary>
    internal class AlarmUtil
    {
        private static string connectionString;
        /// <summary>
        /// 添加报警信息（全自定义模式）
        /// </summary>
        /// <param name="alarmInfo">报警信息实体类</param>
        public static void AddAlarmInfo(AlarmInfo alarmInfo)
        {
            try
            {
                connectionString = ConfigurationManager.AppSettings["MySQLStr"];
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    connection.Open();

                    string query = "INSERT INTO systemlog (log_name, log_type, log_state, log_createtime, log_updatetime) " +
                                   "VALUES (@logName, @logType, @logState, @logCreateTime, @logUpdateTime)";

                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    cmd.Parameters.AddWithValue("@logName", alarmInfo.LogName);
                    cmd.Parameters.AddWithValue("@logType", alarmInfo.LogType);
                    cmd.Parameters.AddWithValue("@logState", alarmInfo.LogState);
                    cmd.Parameters.AddWithValue("@logCreateTime", alarmInfo.LogCreateTime);
                    cmd.Parameters.AddWithValue("@logUpdateTime", alarmInfo.LogUpdateTime);

                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine("Error: " + ex.Message);
            }
        }

        /// <summary>
        /// 添加报警信息（简洁模式）
        /// </summary>
        /// <param name="info">报警信息</param>
        /// <param name="type">报警类型</param>
        public static void AddAlarmInfo(string info)
        {
            // 构造AlarmInfo对象
            AlarmInfo alarmInfo = new AlarmInfo
            {
                LogName = info,
                LogType = 0, // 不知道都有啥，暂且为0吧
                LogState = 0, // 默认未处理：0
                LogCreateTime = DateTime.Now,
                LogUpdateTime = DateTime.Now
            };

            // 调用现有的AddAlarmInfo方法
            AddAlarmInfo(alarmInfo);
        }

        /// <summary>
        /// 添加报警信息（类型自定义模式）
        /// </summary>
        /// <param name="info">报警信息</param>
        /// <param name="type">报警类型</param>
        public static void AddAlarmInfo(string info, int type)
        {
            // 构造AlarmInfo对象
            AlarmInfo alarmInfo = new AlarmInfo
            {
                LogName = info,
                LogType = type,
                LogState = 0, // 默认未处理：0
                LogCreateTime = DateTime.Now,
                LogUpdateTime = DateTime.Now
            };

            // 调用现有的AddAlarmInfo方法
            AddAlarmInfo(alarmInfo);
        }
    }
}
